﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MKDiractorySearch
{
    public class MKDirectorySearch
    {
        /// <summary>
        /// Gets the file sys names.
        /// </summary>
        public IEnumerable<MKFile> FileSysNames
        {
            get
            {
                return GetRecursiveFileNames();
            }
        }

        /// <summary>
        /// Gets the current diractory.
        /// </summary>
        public MKDirectory CurrentDirectory { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="MKDirectorySearch"/> class.
        /// </summary>
        /// <param name="directoryPath">The directory path.</param>
        public MKDirectorySearch(string directoryPath)
        {
            DirectoryInfo dir = new DirectoryInfo(directoryPath);
            this.CurrentDirectory = MKDirectory.Read(directoryPath: directoryPath);
        }

        /// <summary>
        /// Gets the recursive file names.
        /// </summary>
        /// <param name="directoryPath">The directory path.</param>
        /// <returns></returns>
        public IEnumerable<MKFile> GetRecursiveFileNames()
        {
            return this.ReadFiles(this.CurrentDirectory).Where(f => f.Type.Equals(FileType.Video));
        }

        /// <summary>
        /// Reads the files.
        /// </summary>
        /// <param name="dir">The dir.</param>
        /// <returns></returns>
        private IEnumerable<MKFile> ReadFiles(MKDirectory dir)
        {
            List<MKFile> files = new List<MKFile>();
            if (dir.ChildDirectories.Count() > 0)
                files.AddRange(dir.ChildDirectories.SelectMany(m => ReadFiles(m)));
            files.AddRange(dir.ChildFiles);
            return files;
        }
    }
}
